#include "EyerCore/EyerCore.hpp"

#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl3.h"

#include "EyerGLAD/glad.h"

#include <GLFW/glfw3.h>

#include "EWAUI/EWAUIHeader.hpp"
#include "EWAUIWidget/EWAUIWidgetHeader.hpp"

#include "EWAUIWindowDockerSpace.hpp"
#include "EWAUIWidgetTest1.hpp"

int main(int argc,char **argv)
{
    EyerLog("EWA Desktop Start\n");

    Eyer::EWAUIWidget widget(Eyer::EWAUILayoutParams::MATCH_PARENT, Eyer::EWAUILayoutParams::MATCH_PARENT);

    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    GLFWwindow* window = glfwCreateWindow(1280, 720, "Eyer Wand Editor", NULL, NULL);
    if (window == NULL) {
        EyerLog("Failed to create GLFW window\n");
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
        EyerLog("Failed to initialize GLAD\n");
        return -1;
    }


    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImGuiIO& io = ImGui::GetIO();
    io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
    auto fonts = ImGui::GetIO().Fonts;

    /*
    fonts->AddFontFromFileTTF(
            "./HarmonyOS_Sans_SC_Light.ttf",
            13.0f,
            NULL,
            fonts->GetGlyphRangesChineseFull()
    );
    */

    ImGui::StyleColorsDark();
    // ImGui::StyleColorsLight();

    ImGuiStyle& style = ImGui::GetStyle();
    style.WindowRounding = 0;
    style.FrameRounding = 0;
    style.ScrollbarRounding = 0;
    style.TabRounding = 0;

    ImGui_ImplGlfw_InitForOpenGL(window, true);
    ImGui_ImplOpenGL3_Init("#version 330");

    Eyer::EWAUIWindowDockerSpace dockerSpace;
    Eyer::EWAUIWidgetTest1 widgetTest1;

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    while(!glfwWindowShouldClose(window)) {
        glClearColor(0.0, 0.0, 0.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);

        ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplGlfw_NewFrame();
        ImGui::NewFrame();

        // ImGui::ShowDemoWindow();
        dockerSpace.OnGUI();
        widgetTest1.OnGUI();

        ImGui::Render();
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    ImGui_ImplOpenGL3_Shutdown();
    ImGui_ImplGlfw_Shutdown();
    ImGui::DestroyContext();

    glfwTerminate();

    return 0;
}